//
//  LKMyShopDetailViewController.m
//  Mideleague_Agent
//
//  Created by Zhiwang on 2020/5/26.
//  Copyright © 2020 Zhiwang. All rights reserved.
//

#import "LKMyShopDetailViewController.h"
#import "LKMyShopHeaderIntroView.h"
#import "LKMyShopDetailDateView.h"
#import "LKMyShopDetailPriceCell.h"
#import "LKMyShopDetailTableHeaderView.h"
#import "LKShopInformationViewController.h"
#import "TypeMenu.h"
#import "GFDatePicker.h"
@interface LKMyShopDetailViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)LKMyShopDetailDateView *dateView;
@property(nonatomic,strong)LKBaseTableview *tableView;

@property(nonatomic,strong)LKMyShopListDetailModel *detailModel;

@property(nonatomic,strong)LKMyShopHeaderIntroView *headerView;
@end

@implementation LKMyShopDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"商户详情";
    NSString *currentStr = [NSString getCurrentTime];
    NSString *currentTime = [currentStr componentsSeparatedByString:@" "][0];
    
    self.dateView.monthLabel.text = [currentTime componentsSeparatedByString:@"-"][1];
    self.dateView.yearLabel.text = [currentTime componentsSeparatedByString:@"-"][0];
    [self getShopDetailInformationWithMonth:[NSString stringWithFormat:@"%@%@", [currentTime componentsSeparatedByString:@"-"][0],[currentTime componentsSeparatedByString:@"-"][1]]];
    [self createShopHeaderView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadCurrentInfor:) name:@"reloadShopDetail" object:nil];
    // Do any additional setup after loading the view.
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    NSString *currentStr = [NSString getCurrentTime];
//    NSString *currentTime = [currentStr componentsSeparatedByString:@" "][0];
//    self.dateView.monthLabel.text = [currentTime componentsSeparatedByString:@"-"][1];
//    self.dateView.yearLabel.text = [currentTime componentsSeparatedByString:@"-"][0];
//    [self getShopDetailInformationWithMonth:[NSString stringWithFormat:@"%@%@", [currentTime componentsSeparatedByString:@"-"][0],[currentTime componentsSeparatedByString:@"-"][1]]];
}
-(void)reloadCurrentInfor:(NSNotification *)notice{
    NSString *currentStr = [NSString getCurrentTime];
    NSString *currentTime = [currentStr componentsSeparatedByString:@" "][0];
    self.dateView.monthLabel.text = [currentTime componentsSeparatedByString:@"-"][1];
    self.dateView.yearLabel.text = [currentTime componentsSeparatedByString:@"-"][0];
    [self getShopDetailInformationWithMonth:[NSString stringWithFormat:@"%@%@", [currentTime componentsSeparatedByString:@"-"][0],[currentTime componentsSeparatedByString:@"-"][1]]];
}
-(void)createShopHeaderView{
    self.headerView = [[[NSBundle mainBundle] loadNibNamed:@"LKMyShopHeaderIntroView" owner:self options:nil] lastObject];
    _headerView.frame = CGRectMake(0, NAVIGATION_BAR_HEIGHT, WIDTH, 111);
    [_headerView.arrowinBtn addTarget:self action:@selector(goInformationVc:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_headerView];
    self.dateView = [[[NSBundle mainBundle] loadNibNamed:@"LKMyShopDetailDateView" owner:self options:nil] lastObject];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(datePickerViewShow:)];
    [self.dateView.dateView addGestureRecognizer:tap];
    NSString *currentStr = [NSString getCurrentTime];
    NSString *currentTime = [currentStr componentsSeparatedByString:@" "][0];
    
    self.dateView.monthLabel.text = [currentTime componentsSeparatedByString:@"-"][1];
    self.dateView.yearLabel.text = [currentTime componentsSeparatedByString:@"-"][0];
    [self.view addSubview:_dateView];
    [_dateView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(_headerView.mas_bottom).offset(10);
        make.height.mas_equalTo(62);
    }];
    self.tableView = [LKBaseTableview setTableviewWithFrame:CGRectZero style:UITableViewStyleGrouped registercellClasss:@[@"LKMyShopDetailPriceCell"]];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.view addSubview:_tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(_dateView.mas_bottom).offset(10);
        make.bottom.equalTo(self.view).offset(0);
    }];
    [self.tableView reloadData];
}

/// 时间选择器
/// @param tap 点击事件
-(void)datePickerViewShow:(UITapGestureRecognizer *)tap{
    WS(weakSelf)
    [GFDatePicker showCustomDatePickerAtView:[UIApplication sharedApplication].keyWindow choosedDateBlock:^(NSDate *date) {
        NSString *dateStr = [NSString stringWithFormat:@"%@",date];
        NSString *timeStr = [dateStr componentsSeparatedByString:@" "][0];
        weakSelf.dateView.yearLabel.text = [timeStr componentsSeparatedByString:@"-"][0];
        weakSelf.dateView.monthLabel.text = [timeStr componentsSeparatedByString:@"-"][1];
        [weakSelf getShopDetailInformationWithMonth:[NSString stringWithFormat:@"%@%@",[timeStr componentsSeparatedByString:@"-"][0],[timeStr componentsSeparatedByString:@"-"][1]]];
//        [weakSelf getShopDetailInformationwithMonth:[NSString stringWithFormat:@"%@%@",weakSelf.dateView.yearLabel.text,weakSelf.dateView.monthLabel]];
    } cancelBlock:^{
        
    }];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.detailModel.pay_info.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LKMyShopDetailPriceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKMyShopDetailPriceCell"];
    cell.payinforModel = self.detailModel.pay_info[indexPath.section];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 52.0f;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    LKMyShopDetailTableHeaderView *header = [[[NSBundle mainBundle] loadNibNamed:@"LKMyShopDetailTableHeaderView" owner:self options:nil] lastObject];
    header.backgroundColor = COLOR_HEX(0xFDF8F5);
    return header;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 52.f;
    }
    return 0.000000001f;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footerView = [[UIView alloc] init];
    if (section == self.detailModel.pay_info.count - 1) {
        LKBaseLabel *introLabel = [LKBaseLabel labelWithFrame:CGRectZero text:@"提示：统计数据截止到昨日" textColor:[UIColor lightGrayColor] font:[UIFont fontWithName:@"PingFangSC-Regular" size:12] textAlignment:NSTextAlignmentCenter];
        [footerView addSubview:introLabel];
        [introLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(footerView);
            make.top.equalTo(footerView).offset(20);
        }];
    }

    return footerView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == self.detailModel.pay_info.count - 1) {
        return 22;
    }
    return 1.0f;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}

/// 获取商户详细信息
-(void)getShopDetailInformationWithMonth:(NSString *)month{
    [SVProgressHUD show];
    NSDictionary *param = @{@"mch_id":self.listModel.mch_id,@"merchant_phone":self.listModel.merchant_phone,@"mouth":month};
    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:MyShopDetailUrl header:[LKUserInforTool account].token parameters:param progress:^(NSProgress * _Nullable progress) {
        
    } success:^(BOOL isSuccess, id  _Nullable responseObject) {
        [SVProgressHUD dismiss];
        if ([responseObject[@"code"] isEqualToString:@"success"]) {
            self.detailModel = [LKMyShopListDetailModel modelWithDictionary:responseObject[@"data"]];
            self.headerView.detailModel = self.detailModel;
            self.dateView.detailModel = self.detailModel;
            NSMutableArray *inforArray = [[NSMutableArray alloc] init];
            for (NSInteger i = 0; i < self.detailModel.pay_info.count; i++) {
                LKPayInforModel *payModel = self.detailModel.pay_info[i];
                if ([payModel.pay_style containsString:@"微信支付"] || [payModel.pay_style containsString:@"支付宝支付"] || [payModel.pay_style containsString:@"银联云闪付"]) {
                    [inforArray addObject:payModel];
                }else{

                }
            }
            self.detailModel.pay_info = inforArray;
            [self.tableView reloadData];
        }else{
            [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
        }
    } failure:^(NSString * _Nullable errorMessage) {
        
    }];
}
//跳转信息编辑页面
-(void)goInformationVc:(UIButton *)btn{
    LKShopInformationViewController *shopVc = [[LKShopInformationViewController alloc] init];
    if ([self.listModel.agency_business containsString:@"3"]) {
        shopVc.showRight = YES;
    }
    if ([self.listModel.agency_business containsString:@"1"]) {
        shopVc.showLeft = YES;
    }
    shopVc.shoplistModel = self.listModel;
    [self.navigationController pushViewController:shopVc animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
